package me.zhengjie.modules.management.job;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.management.domain.RoomDeptView;
import me.zhengjie.modules.management.mapper.DeptMapper;
import me.zhengjie.modules.management.repository.RoomDeptViewRepository;
import me.zhengjie.modules.system.domain.Dept;
import org.springframework.stereotype.Component;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


/**
 * @website https://docs.auauz.net
 * @Description:
 * @Auther: chao.ji
 * @Date: 2020/12/22
 */
@Component
@RequiredArgsConstructor
public class DeptJob extends IJobHandler {
    private final RoomDeptViewRepository roomDeptViewRepository;
    private final DeptMapper deptMapper;

    @XxlJob("mr-dept")
    @Override
    public ReturnT<String> execute(String s) throws Exception {
            List<RoomDeptView> list=roomDeptViewRepository.findA();

            for (RoomDeptView roomDeptView : list) {
                //不存在，插入
                if(deptMapper.findById(roomDeptView.getOid().toString())==null){
                    Dept dept=new Dept();
                    dept.setFullName(roomDeptView.getName());
                    dept.setCode(roomDeptView.getCode());
                    dept.setName(roomDeptView.getShort_name());
                    dept.setId(roomDeptView.getOid().toString());
                    dept.setPid((roomDeptView.getPoid_org_admin()).toString());
                    dept.setCreateTime(new Timestamp(new Date().getTime()));
                    deptMapper.insertDept(dept);

                }else{
                    Dept dept=new Dept();
                    //存在，更新
                    dept.setId(roomDeptView.getOid().toString());
                    dept.setFullName(roomDeptView.getName());
                    dept.setCode(roomDeptView.getCode());
                    dept.setName(roomDeptView.getShort_name());
                    dept.setPid((roomDeptView.getPoid_org_admin()).toString());
                    dept.setUpdateTime(new Timestamp(new Date().getTime()));
                    deptMapper.updateDept(dept);
                    }
                }
        return ReturnT.SUCCESS;
    }

}

